package com.gl.glcourses.repository.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.gl.glcourses.repository.IStaticRepository;
import com.gl.glcourses.vos.IValueObject;

@Repository
public class JpaStaticRepository<T> implements IStaticRepository<T> {

	@PersistenceContext
	protected EntityManager em;
	
	@Override
	public List<T> listStatic(Class clase) {
		Query query = null;
		query = em.createQuery("FROM " + clase.getSimpleName());
		return query.getResultList();
	}

	@Override
	public void saveStatic(T object) throws Exception {
		em.persist(object);
	}
	
	@Override
	public T saveUpdate(T object) throws Exception {
		Object merge = em.merge(object);
		((IValueObject)object).setId( ((IValueObject)merge).getId() );
		return object;
	}

}
